package mo.tags.stack;

import java.util.Stack;

public class L1574 {

    public int findLengthOfShortestSubarray(int[] arr) {
        int len = arr.length;
        int r = 1;
        while (r < len && arr[r] >= arr[r-1]) {
            r++;
        }
        if (r == len) {
            return 0;
        }
        int l = len - 2;
        while (arr[l] <= arr[l+1]) {
            l--;
        }
        int ans = Integer.MAX_VALUE;
        for (int i = l+1, j = 0; i < len; i++) {
            for (; j < r && arr[j] <= arr[i]; j++) {
            }
            ans = Math.min(ans, i-j);
        }
        ans = Math.min(ans, Math.min(len-r, l+1));
        return ans;
    }

    public static void main(String[] args) {
        L1574 test = new L1574();
        System.out.println(test.findLengthOfShortestSubarray(new int[]{1,2,3,10,4,2,3,5}));
    }

}
